package com.sl.plat.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sl.common.ResponseData;
import com.sl.common.exception.GlobalErrorCodeEnum;
import com.sl.water.dto.PlanDynamicDTO;
import com.sl.water.entity.PlanDynamic;
import com.sl.water.entity.PlanECunPerson;
import com.sl.water.entity.PlanEWz;
import com.sl.water.entity.RehearsalScheme;
import com.sl.water.service.*;
import com.sl.water.vo.AttachVO;
import com.sl.water.vo.PlanAzqListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @description:预案接口
 * @author: duan
 * @time: 2025-06-26 14:09
 */

@RestController
@RequestMapping("/plat/plan")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@Tag(name = "预案-动态预案", description = "动态预案")
public class PlanDynamicController {

    private final AttachService attachService;
    private final PlanDynamicService planDynamicService;
    private final PlanTransferService planTransferService;
    private final PlanControlFhzService planControlFhzService;
    private final PlanEWzService planEWzService;
    private final PlanECunPersonService planECunPersonService;
    private final PlanEAzqService planEAzqService;
    private final PlanEAzlxService planEAzlxService;
    private final RehearsalSchemeService rehearsalSchemeService;


    @GetMapping("/createByRehearsalId")
    @Operation(summary = "根据预演ID生成预案信息")
    public ResponseData<Long> createByYY(
            @Parameter(description = "预演ID", example = "1")
            @RequestParam Long rehearsalId
    ) {

        PlanDynamic planDynamic = planDynamicService.createByYY(rehearsalId);
        return ResponseData.success(planDynamic.getId());
    }

    @GetMapping("/dynamic/pageList")
    @Operation(summary = "预案分页列表")
    public ResponseData<Page<PlanDynamic>> getDynamicPageList(
            @Parameter(
                    description = "页码",
                    example = "1", required = true
            )
            @RequestParam(defaultValue = "1") int page,
            @Parameter(
                    description = "分页数量",
                    example = "10", required = true
            )
            @RequestParam(defaultValue = "10") int size,
            @Parameter(
                    description = "名称"
            ) @RequestParam(required = false) String name) {
        return ResponseData.success(planDynamicService.pageList(page, size, name));
    }

    @GetMapping("/{id}")
    @Operation(summary = "预案信息查询", description = "返回预案、分洪闸调度、避洪转移方案")
    public ResponseData<PlanDynamicDTO> get(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        return ResponseData.success(planDynamicService.getPlanDynamicDTOById(id));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除")
    public ResponseData removeById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        planDynamicService.removeData(id);
        return ResponseData.success("删除成功");
    }

    @GetMapping("/genReport/{id}")
    @Operation(summary = "生成预案报告")
    public ResponseData create(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        //TODO：需求清楚后实现
        return ResponseData.success("生成预案报告成功");
    }

    @PutMapping("/updateStatus/{id}")
    @Operation(summary = "修改状态")
    public ResponseData updateStatusById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id,
            @Parameter(
                    description = "状态(0未发布,1,已发布)",
                    required = true,
                    example = "0"
            )
            @RequestParam Integer state) {

        planDynamicService.updateState(id, state);
        return ResponseData.success("修改状态成功");
    }


    @PostMapping("/templateUpload")
    @Operation(summary = "报告模板上传")
    @ApiResponse(responseCode = "200", description = "上传成功", content = @Content(schema = @Schema(implementation = AttachVO.class)))
    public ResponseEntity<?> upload(
            @Parameter(description = "文件", required = true)
            @RequestPart MultipartFile file) {
        return attachService.upload("plan_dynamic", "template.pdf", file);
    }

    @GetMapping("/templateDownload")
    @Operation(summary = "报告模板下载")
    public void downloadFile(HttpServletResponse response) {
        attachService.downloadFile("plan_dynamic/template.pdf", response);
    }

    @GetMapping("/templatePreview")
    @Operation(summary = "报告模板预览")
    public ResponseEntity<byte[]> previewFile(HttpServletResponse response) {
        return attachService.previewFile("plan_dynamic/template.pdf", response);
    }

    @GetMapping("/wz/pageList")
    @Operation(summary = "物资分页列表")
    public ResponseData<Page<PlanEWz>> getWzPageList(
            @Parameter(
                    description = "页码",
                    example = "1", required = true
            )
            @RequestParam(defaultValue = "1") int page,
            @Parameter(
                    description = "分页数量",
                    example = "10", required = true
            )
            @RequestParam(defaultValue = "10") int size,
            @Parameter(
                    description = "物资库"
            ) @RequestParam(required = false) String wzk) {
        return ResponseData.success(planEWzService.pageList(page, size, wzk));
    }


    @GetMapping("/person/list")
    @Operation(summary = "人员列表", description = "按照村和类型查询列表")
    public ResponseData<List<PlanECunPerson>> getPersonList(
            @Parameter(
                    description = "村代码",example = "370523101221"
            ) @RequestParam(required = false) String cun,
            @Parameter(
                    description = "类型",example = "值班值守"
            ) @RequestParam(required = false) String kind) {
        return ResponseData.success(planECunPersonService.getPersonList(cun, kind));
    }

    @GetMapping("/getAzqGeojson")
    @Operation(summary = "安置区Geojson")
    public ResponseData getAzqGeojson (
            @Parameter(
                    description = "安置区名称",example = "安置区1"
            ) @RequestParam(required = true) String name) {
        return ResponseData.success(JSONUtil.parse(this.planEAzqService.getGeoJson(name)));
    }

    @GetMapping("/getZylxGeojson")
    @Operation(summary = "转移路线Geojson")
    public ResponseData getZylxGeojson (
            @Parameter(
                    description = "转移路线名称",example = "转移路线1"
            ) @RequestParam(required = true) String name,
            @Parameter(
                    description = "预案ID",example = "1"
            ) @RequestParam(required = true) long id) {
        PlanDynamic plan =this.planDynamicService.getById(id);
        if(plan==null)
            return ResponseData.error(GlobalErrorCodeEnum.BAD_REQUEST,"预案不存在");
        RehearsalScheme scheme = this.rehearsalSchemeService.getScheme(plan.getRehearsalId());
        if(scheme==null)
            return ResponseData.error(GlobalErrorCodeEnum.BAD_REQUEST,"预案关联预演方案不存在");
        return ResponseData.success(JSONUtil.parseArray(this.planEAzlxService.getGeoJson(name,scheme._getZylxKind())));
    }


}
